package com.aistar.controller;

import com.aistar.pojo.*;
import com.aistar.service.*;
import com.aistar.util.ServerResponse;
import org.apache.http.HttpRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
public class SeckillProductDetailController {
    Logger logger = LoggerFactory.getLogger(SeckillProductDetailController.class);

    @Autowired
    private SeckillDetailService seckillDetailService;
    @Autowired
    private SeckillService seckillService;
    @Autowired
    private ProductSpecService productSpecService;

    @Autowired
    private CartService cartService;
    @Autowired
    private CartDetailsService cartDetailsService;


    @GetMapping("/goodsDetail/{id}")
    @ResponseBody
    public ModelAndView getProDetail( @PathVariable("id") Integer proId){
        logger.info("proId:"+proId);
        ModelAndView mav = new ModelAndView("/product/SeckillProductDetail");
        ServerResponse product = seckillDetailService.getGoodsById(proId);
        ServerResponse seckill = seckillDetailService.getSeckillByGId(proId);
        ServerResponse productImages = seckillDetailService.getGoodsImgByGId(proId);
        ServerResponse prospec = seckillDetailService.getSepecByGId(proId);

        System.out.println(product);
        System.out.println(seckill);
        System.out.println(productImages);
        System.out.println(prospec);

        mav.addObject("product",product.getData());
        mav.addObject("seckill",seckill.getData());
        mav.addObject("proImg",productImages.getData());
        mav.addObject("spec",prospec.getData());
        System.out.println(prospec.getData());
        return mav;
    }

    @GetMapping("/getSpec")
    @ResponseBody
    public ServerResponse getSpecDate(Integer proId){
         return productSpecService.getByProId(proId);
    }

    //http://localhost:8080/fresh/seckill/addcart
    @PostMapping("/seckill/addcart")
    @ResponseBody
    public ServerResponse addcart(Integer proId, HttpServletRequest request ){
        Customer customer = (Customer)request.getSession().getAttribute("loginCustomer");
        Integer cusId = customer.getCustomerId();
        ServerResponse serverResponse = cartService.getCartIdByCustomerId(cusId);
        System.out.println("serverResponse::::"+serverResponse);
        if (serverResponse.getStatus()==1){
            Integer cartId = (Integer)serverResponse.getData();
            System.out.println("cartId:::"+cartId);

            CartDetails cartDetails = new CartDetails();
            cartDetails.setCartId(cartId);
            cartDetails.setProductId(proId);
            cartDetails.setCartDetailsSize(1);
            cartDetails.setCartDetailsTime(new Date());
            cartDetails.setCartDetailsStatus(1);
            ServerResponse serverResponse1 = cartDetailsService.save(cartDetails);
            if (serverResponse1.getStatus()==1){
                return serverResponse1;
            }
            return serverResponse1;
        }
        return serverResponse;
    }
}
